/*
-------------- function index --------------
trim       function:                                        trim() lTrim() rTrim()
realLength function                                         realLength()
isEmpty    function:                                        isEmpty()
isNotEmpty function:                                        isNotEmpty()
check a string is integer:                                  checkIsInteger(str)
check a string is integer and not less the other value:     checkIntegerMinValue(str,val)
check a string is integer and not bigger the other value:   checkIntegerMaxValue(str,val) 
check a string is not negative integer:             		isNotNegativeInteger(str)
check a string is double:            						checkIsDouble(str) 
check a string is double and not less the other value:      checkDoubleMinValue(str,val)
check a string is double and not bigger the other value:    checkDoubleMaxValue(str,val)
check a string is not negative double:            			isNotNegativeDouble(str)
check a string is date:           							checkIsValidDate(str)
compare two date:              						 		checkDateEarlier(strStart,strEnd)
check a string is email:         							checkEmail(str)
check a string is URL:         							    checkUrl(str)
check a string is IP:         							    checkIp(str)
check a string is chinese:             						checkIsChinese(str)
calculate string length,a chinese is two char:   			realLength()
clear Form Input                                            clearFormInput()
-------------- function index --------------
*/

/********************************** Trim **************************************/
/**
* remove space
* trim:remove both side space lTrim:remove left side space rTrim: remove right side space
* guide:
*     var str = " hello ";
*     str = str.trim();
*/
String.prototype.trim = function () {
	return this.replace(/(^[\s]*)|([\s]*$)/g, "");
};
String.prototype.lTrim = function () {
	return this.replace(/(^[\s]*)/g, "");
};
String.prototype.rTrim = function () {
	return this.replace(/([\s]*$)/g, "");
};
/**
* calculate the string length,chinese is two char
*/
String.prototype.realLength = function () {
	return this.replace(/[^\x00-\xff]/g, "**").length;
};
/*--------------------------------- Trim --------------------------------------*/
/********************************** Empty **************************************/
/**
*check the string is  empty
*return:
*if the string is empty,pass check,  return true
*if the string is not empty,         return false               
*/
String.prototype.isEmpty = function () {
	if (this.trim() == "") {
		return true;
	} else {
		return false;
	}
};
/**
*check the string is not empty
*return:
*if the string is not empty,pass check,  return true
*if the string is empty,                 return false               
*/
String.prototype.isNotEmpty = function () {
	if (this.trim() == "") {
		return false;
	} else {
		return true;
	}
};

/*--------------------------------- Empty --------------------------------------*/
/********************************** Undefined **************************************/
/**
*check the string is defined
*return:
*if the string is defined, pass check,  return true
*if the string is undefined,            return false               
*/
function checkIsDefined(element) {
	if (element == undefined) {
		return false;
	} else {
		return true;
	}
}
/*--------------------------------- Undefined --------------------------------------*/
/********************************** Integer *************************************/
/**
*check the string is Integer
*return:
*if the string is null, could not pass chesk,          return false
*if the string is not Integer,pass check,              return true
*if the string is Integer,                             return false               
*/
function checkIsInteger(str) {
	if (str == "") {
		return false;
	}
	if (/^(\-?)(\d+)$/.test(str)) {
		return true;
	} else {
		return false;
	}
}
/**
*check the str is Integer and it bigger than the specify val
*return:
*if the string is null, could not pass chesk,                                return false
*if the string is Integer and it bigger than the specify val ,pass check,      return true
*else 												                         return false               
*/
function checkIntegerMinValue(str, val) {
	if (str == "") {
		return false;
	}
	if (typeof (val) != "string") {
		val = val + "";
	}
	if (checkIsInteger(str) == true) {
		if (parseInt(str, 10) >= parseInt(val, 10)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
/**
*check the str is Integer and it less than the specify val
*return:
*if the string is null, could not pass chesk,                                return false
*if the string is Integer and it less than the specify val ,pass check,      return true
*else 												                         return false               
*/
function checkIntegerMaxValue(str, val) {
	if (str == "") {
		return false;
	}
	if (typeof (val) != "string") {
		val = val + "";
	}
	if (checkIsInteger(str) == true) {
		if (parseInt(str, 10) <= parseInt(val, 10)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
/**
*check the string is not a negative Integer
*return:
*if the string is null, definition could not pass chesk,    return false
*if the string is is not a negative Integer, pass check,    return true
*else														return false					
*/
function isNotNegativeInteger(str) {
	if (str == "") {
		return false;
	}
	if (checkIsInteger(str) == true) {
		if (parseInt(str, 10) < 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}
/**
*check the string is not a negative Integer
*return:
*if the string is null, definition could not pass chesk,    return false
*if the string is is not a negative Integer, pass check,    return true
*else														return false					
*/
function isNotNegativeMaxInteger(str,val) {
	if (str == "") {
		return false;
	}
	if (checkIsInteger(str) == true) {
		var inValue = parseInt(str, 10);
		if (inValue >= 0 && inValue <= parseInt(val, 10)) {
			return true;
		} else{
			return false;
		}
	} else {
		return false;
	}
}
/*
*check the string is Natural Number
*/
function checkNaturalNum(str){
  return /^[1-9]\d*$/g.test(str);
}
/**
*cast the string to integer
*return:
*if the string is null or undefined,                        return 0
*if the string is a Integer,							    return value
*else														return 0					
*/
function changeStrIntoInteger(str){
	var numStr = str.trim();
	if(numStr=="" || numStr=="undefined")
		return 0;
	if(checkIsInteger(numStr)){
	    return parseInt(numStr, 10);
	 }
	 else return 0;	        
}
/*--------------------------------- Integer --------------------------------------*/
/********************************** Double ****************************************/
/**
*check the string is Double
*return:
*if the string is null, could not pass chesk,          return false
*if the string is not Double,pass check,               return true
*if the string is Double,                              return false               
*/
function checkIsDouble(str) {
	if (str == "") {
		return true;
	}
	if (str.indexOf(".") == -1) {
		if (checkIsInteger(str) == true) {
			return true;
		} else {
			return false;
		}
	} else {
		if (/^(\-?)(\d+)(.{1})(\d+)$/g.test(str)) {
			return true;
		} else {
			return false;
		}
	}
}
/**
*check the str is Double and it bigger than the specify val
*return:
*if the string is null, could not pass chesk,                                return false
*if the string is Double and it bigger than the specify val ,pass check,       return true
*else 												                         return false               
*/
function checkDoubleMinValue(str, val) {
	if (str == "") {
		return true;
	}
	if (typeof (val) != "string") {
		val = val + "";
	}
	if (checkIsDouble(str) == true) {
		if (parseFloat(str) >= parseFloat(val)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
/**
*check the str is Double and it less than the specify val
*return:
*if the string is null, could not pass chesk,                                return false
*if the string is Double and it less than the specify val ,pass check,       return true
*else 												                         return false               
*/
function checkDoubleMaxValue(str, val) {
	if (str == "") {
		return true;
	}
	if (typeof (val) != "string") {
		val = val + "";
	}
	if (checkIsDouble(str) == true) {
		if (parseFloat(str) <= parseFloat(val)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
/**
*check the string is not a negative double
*return:
*if the string is null, definition could not pass chesk,    return false
*if the string is is not a negative double, pass check,     return true
*/
function isNotNegativeDouble(str) {
	if (str == "") {
		return false;
	}
	if (checkIsDouble(str) == true) {
		if (parseFloat(str) < 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}
/**
*cast the string to Double
*return:
*if the string is null or undefined,                        return 0
*if the string is a Double,							    return value
*else														return 0					
*/
function changeStrIntoDouble(str){
	var numStr = str.trim();
	if(numStr=="" || numStr=="undefined")
		return 0;
	if(checkIsDouble(numStr)){
	    return parseFloat(numStr);
	 }
	 else return 0;	        
}
/*--------------------------------- Double ---------------------------------------*/
/********************************** Date ******************************************/
/**
*check the string is a date, data format is yyyy-MM-dd
*return:
*if the string is null, define pass chesk,          return true
*if the string is date, pass check,                  return true
*if the string is a invalid date,                    return false
*refrance information:input value is invalid!(yyyy-MM-dd)
*/
function checkIsValidDate(str) {
	if (str == "") {
		return true;
	}
	var pattern = /^((\d{4})|(\d{2}))-(\d{1,2})-(\d{1,2})$/g;
	if (!pattern.test(str)) {
		return false;
	}
	var arrDate = str.split("-");
	if (parseInt(arrDate[0], 10) < 100) {
		arrDate[0] = 2000 + parseInt(arrDate[0], 10) + "";
	}
	var date = new Date(arrDate[0], (parseInt(arrDate[1], 10) - 1) + "", arrDate[2]);
	if (date.getYear() == arrDate[0] && date.getMonth() == (parseInt(arrDate[1], 10) - 1) + "" && date.getDate() == arrDate[2]) {
		return true;
	} else {
		return false;
	}
}
/**
*check the string is a date, data format is yyyy-MM-dd HH:MM:SS
*return:
*if the string is null, define pass chesk,               return false
*if the string is date, pass check,                      return true
*if the string is a invalid date,                        return false
*if the string is a  date but bigger than present date,  return false
*refrance information:input value is invalid!(yyyy-MM-dd HH:MM:SS)
*/
function checkDate(DateStr) {
	var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	var sR = DateStr.match(reg);
	if (sR == null) {
		alert("\u8f93\u5165\u8fc7\u6ee4\u65f6\u95f4\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff01\u65e5\u671f\u683c\u5f0f\uff1a2006-09-15 16:20:06");
		return false;
	}
	var startdate = new Date(sR[1], sR[3] - 1, sR[4], sR[5], sR[6], sR[7]);
	if (startdate > new Date()) {
		alert("\u8f93\u5165\u8fc7\u6ee4\u65f6\u95f4\u9519\u8bef\uff0c\u8f93\u5165\u65f6\u95f4\u4e0d\u80fd\u5927\u4e8e\u5f53\u524d\u65f6\u95f4\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff01");
		return false;
	} else {
		return true;
	}
}
/**
*compay the two string date, data format is yyyy-MM-dd HH:MM:SS
*return:
*if the two string have one that value is  null,         return false
*if the string is date, pass check,                      return true
*if the string is a invalid date,                        return false
*if the string is a  date but bigger than present date,  return false
*refrance information:input value is invalid!(yyyy-MM-dd HH:MM:SS)
*/
function compareDate(startDateStr, endDateStr) {
	var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	var sR = startDateStr.match(reg);
	var eR = endDateStr.match(reg);
	if (sR == null || eR == null) {
		alert("\u8f93\u5165\u6570\u636e\u9519\u8bef\uff0c\u6709\u4e00\u4e2a\u6216\u4e24\u4e2a\u8f93\u5165\u6570\u636e\u662f\u7a7a\uff01");
		return;
	}
	var startdate = new Date(sR[1], sR[3] - 1, sR[4], sR[5], sR[6], sR[7]);
	var enddate = new Date(eR[1], eR[3] - 1, eR[4], eR[5], eR[6], eR[7]);
	return startdate < enddate;
}
/*--------------------------------- Date ----------------------------------------*/
/********************************** Mail *****************************************/
function checkEmail(str) {
	return str == "" ? false : /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
}
/********************************** Url *****************************************/
function checkUrl(str) {
	var urlStr = str.trim();
	if (urlStr.indexOf("http://") == 0 || urlStr.indexOf("https://") == 0) {
		return true;
	} else {
		alert("Please Input Url with Protocol of http:// or https:// ");
		return false;
	}
}
/********************************** Ip *****************************************/
function checkIP(str) {
	return str == "" ? false : /^(([01]?[\d]{1,2})|(2[0-4][\d])|(25[0-5]))(\.(([01]?[\d]{1,2})|(2[0-4][\d])|(25[0-5]))){3}$/.test(str);
}
function trimPreZero(str) {
	return str.replace(/0(\d)/g, "$1");
}
/********************************** Chinese ***************************************/
/**
*check the string is a chinese
*return:
*if the  string is  null,        						 return false
*if the string is chinese , pass check,                  return true
*if the string is not a chinese,                         return false
*/
function checkIsChinese(str) {
	if (str == "") {
		return false;
	}
	var pattern = /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi;
	if (pattern.test(str)) {
		return true;
	} else {
		return false;
	}
}
/**
*check the string is a English
*return:
*if the  string is  null,        						 return false
*if the string is English , pass check,                  return true
*if the string is not a English,                         return false
*/
function checkIsEnglish(name) {
	if (name.length == 0) {
		return false;
	}
	for (i = 0; i < name.length; i++) {
		if (name.charCodeAt(i) > 128) {
			return false;
		}
	}
	return true;
}
/**********************************check file type*********************************************/
/**
*check a file's Postfix type
*return:
*if the  oFile or type is  null,        				 return false
*if the file Postfix is the given type,                  return true
*if the file Postfix is not the given type,              return false
*/

function checkFilePostfix(oFile,type) {
	if (oFile == null || type==null) {
		return false;
	}	
	var pattern = /(.*)\.(.*)$/gi;
	if (typeof (oFile) == "object") {
		if (oFile.value == null || oFile.value == "") {
			return false;
		}
		var arr = pattern.exec(oFile.value);
		if(type.toLowerCase() == RegExp.$2.toLowerCase())return true;
		else return false;
	} else {
		if (typeof (oFile) == "string") {
			var arr = pattern.exec(oFile);				
			if(type.toLowerCase() == RegExp.$2.toLowerCase())return true;else return false;
		} else {
			return false;
		}
	}
}
/********************************** clear Form Input ***************************************/
/**
*clear input of formObj , except name is exceptObjName
*return:
*require:input must have name
*/
function clearFormInput(formObj,exceptObjName){
	if(formObj==null) return;
	if(exceptObjName==null) exceptObjName=="";
	var selectObjs=formObj.getElementsByTagName("SELECT");//For Select Obj
	for(var i=0;i<selectObjs.length;i++)
	{
		if((selectObjs[i].name=="")||(eval("/(^|,)"+selectObjs[i].name+"(,|$)/g").test(exceptObjName))) continue;
		selectObjs[i].options[0].selected = "selected";
	}
	
	var inputObjs=formObj.getElementsByTagName("INPUT");//For Input Obj
	for(var i=0;i<inputObjs.length;i++)
	{
		if((inputObjs[i].name=="")||(eval("/(^|,)"+inputObjs[i].name+"(,|$)/g").test(exceptObjName))) continue;
		if((inputObjs[i].type.toUpperCase()=="TEXT")||
		   (inputObjs[i].type.toUpperCase()=="HIDDEN")) 
		     inputObjs[i].value="";
		else if((inputObjs[i].type.toUpperCase()=="RADIO")||
				 (inputObjs[i].type.toUpperCase()=="CHECKBOX"))
		         inputObjs[i].checked=false;
	}
	
	var textareaObjs=formObj.getElementsByTagName("TEXTAREA");//For textarea Obj
	for(var i=0;i<textareaObjs.length;i++)
	{
		if((textareaObjs[i].name=="")||(eval("/(^|,)"+textareaObjs[i].name+"(,|$)/g").test(exceptObjName))) continue;
		textareaObjs[i].value="";
	}
}